/*
 * @Date: 2020-05-22 23:07:16
 * @LastEditors: 小枫
 * @LastEditTime: 2020-09-17 10:31:44
 * @FilePath: \feng-blog\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    redirect: '/'
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home')
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login'),
    beforeEnter: (to, from, next) => {
      if (window.localStorage.getItem('token')) {
        // console.log(to, from);
        next('/')
      } else {
        next()
      }
    }
  },
  {
    path: '/articles/:aid',
    name: 'Articles',
    component: () => import('../views/ArticleDetail'),
    props: (route) => ({
      aid: parseInt(route.params.aid)
    })
  },
  {
    path: '/class',
    name: 'Class',
    component: () => import('../views/Classification')
  },
  {
    path: '/tag/:tag',
    name: 'Tag',
    component: () => import('../views/TagList'),
    props: (route) => ({
      tag: route.params.tag
    })
  },
  {
    path: '/editor',
    name: 'Editor',
    component: () => import('../views/Editor'),
    beforeEnter: (to, from, next) => {
      if (window.localStorage.getItem('token')) {
        next()
      } else {
        next('/login')
      }
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register')
  }
]


const router = new VueRouter({
  routes
})

export default router
